// pages/address/fill/fill.js
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */

  data: {
    region: ['广东省', '深圳市', '龙华区'],
    nameVal: '',
    mobile: '',
    street: ''
  },
  //清空信息
  empty: function (e) {
    var type = e.target.id;
    if (type == 'realname') {
      this.setData({
        nameVal: ''
      })
    } else if (type == 'mobile') {
      this.setData({
        mobile: ''
      })
    } else if (type == 'street') {
      this.setData({
        street: ''
      })
    }

  },
  //表单提交
  formSubmit: function (e) {
    // console.log(e);
    var that = this;
    var rooturl = app.globalData.url;
    var mobile = e.detail.value.mobile;
    var realname = e.detail.value.realname;
    var address = e.detail.value.address;
    var street = e.detail.value.street;
    var regular = /0?(13|14|15|17|18|19)[0-9]{9}/;

    if (realname == '') {
      wx.showToast({
        title: '请填写收件人姓名',
        icon: 'clear',
        duration: 1500
      })
      return false;
    }
    if (mobile == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'clear',
        duration: 1500
      })
      return false;
    } else if (!(regular.test(mobile))) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'clear',
        duration: 1500
      })
      return false;
    }

    wx.request({
      url: rooturl + 'member_address.php?type=add',
      data: {
        realname: realname,
        mobile: mobile,
        address: address,
        street: street
      },
      header: { 'content-type': 'application/json' },
      method: 'POST',
      dataType: 'json',
      success: (result) => {
        console.log(result);
        if (result.data.status == 1) {
          wx.navigateTo({
            url: "../../address/address"
          });
        }
      },
      // fail: ()=>{},
      // complete: ()=>{}
    });

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(123223);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})